<template>
  <div>
    <!-- 
      用于磁性材料数据库的高级查询组件
      该组件使用方式
      在qsall.vue文件里面，ctrl+f搜索高级查询，把两个注释包围起来的代码复制到想用的页面即可
     -->
    <a-tooltip v-if="superQueryFlag" :mouseLeaveDelay="0.2">
      <template slot="title">
        <span>已有高级查询条件生效</span>
        <a-divider type="vertical" />
        <a @click="resetQuery">清空</a>
      </template>
      <el-button
        icon="el-icon-loading"
        type="primary"
        size="mini"
        @click="
          showQueryDialog = true
          getRefFromFather()
        "
      >
        高级查询
      </el-button>
    </a-tooltip>
    <el-button
      v-else
      type="primary"
      size="mini"
      @click="
        showQueryDialog = true
        getRefFromFather()
      "
      >高级查询</el-button
    >
    <div class="queryCSS">
      <el-dialog title="高级查询构造器" :visible.sync="showQueryDialog" width="70%">
        <div style="margin-bottom: 40px">
          <el-divider></el-divider>
        </div>
        过滤条件选择:
        <el-select v-model="relation" placeholder="请选择" clearable style="width: 200px; margin-bottom: 10px">
          <el-option v-for="item in relationOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
        </el-select>
        <div v-for="index of QueryLength.length" :key="index">
          <el-row :gutter="10" style="margin-bottom: 10px">
            <el-col :span="5">
              <el-select v-model="selectedHeader[index - 1]" @change="headerChanged(index)" placeholder="请选择查询字段" clearable>
                <el-option v-for="item in headerCondition[index - 1]" :key="item.value" :label="item.label" :value="item.value"> </el-option>
              </el-select>
            </el-col>
            <el-col :span="4">
              <el-select v-model="compara[index - 1]" @change="comparaChanged(index)" clearable style="margin: 0 10px">
                <el-option v-for="item in comparaCondition[index - 1]" :key="item.value" :label="item.label" :value="item.value"> </el-option>
              </el-select>
            </el-col>
            <el-col :span="4">
              <el-input style="margin-right: 10px" clearable v-model="fieldValue[index - 1]"></el-input>
            </el-col>
            <el-col :span="2">
              <el-button @click="addCondition" type="primary"><i class="el-icon-plus"></i></el-button>
            </el-col>
            <el-col :span="2">
              <el-button @click="delCondition(index)"><i class="el-icon-minus"></i></el-button>
            </el-col>
          </el-row>
        </div>
        <div style="margin-top: 30px">
          <el-divider></el-divider>
        </div>
        <span>
          <el-button @click="resetQuery">重置</el-button>
        </span>
        <span style="float: right">
          <el-button @click="showQueryDialog = false">取 消</el-button>
          <el-button type="primary" @click="QueryForData">查询</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { Message } from 'element-ui'
export default {
  props: {},
  data() {
    return {
      superQueryFlag: false,
      showQueryDialog: false,
      relation: '',
      relationOptions: [
        {
          label: '所有条件都要满足',
          value: 'and'
        },
        {
          label: '满足任意一个条件',
          value: 'or'
        }
      ],
      QueryLength: [''],
      fieldValue: [''],
      compara: [''],
      comparaOptions: [
        {
          value: '等于',
          label: '等于'
        },
        {
          value: '包含',
          label: '包含'
        },
        // {
        //   value: '以...开始',
        //   label: '以...开始'
        // },
        // {
        //   value: '以...结尾',
        //   label: '以...结尾'
        // },
        {
          value: '不等于',
          label: '不等于'
        },
        {
          value: '大于',
          label: '大于'
        },
        {
          value: '大于等于',
          label: '大于等于'
        },
        {
          value: '小于',
          label: '小于'
        },
        {
          value: '小于等于',
          label: '小于等于'
        }
      ],
      comparaCondition: [
        [
          {
            value: '等于',
            label: '等于'
          },
          {
            value: '包含',
            label: '包含'
          },
          // {
          //   value: '以...开始',
          //   label: '以...开始'
          // },
          // {
          //   value: '以...结尾',
          //   label: '以...结尾'
          // },
          {
            value: '不等于',
            label: '不等于'
          },
          {
            value: '大于',
            label: '大于'
          },
          {
            value: '大于等于',
            label: '大于等于'
          },
          {
            value: '小于',
            label: '小于'
          },
          {
            value: '小于等于',
            label: '小于等于'
          }
        ]
      ],
      selectedHeader: [''],
      headeroptions: [],
      headerCondition: []
    }
  },
  methods: {
    //调用父组件获取主表ref信息的函数
    getRefFromFather() {
      this.$emit('getRef')
    },
    // 获取elements表头
    getTableColumn(tableRef) {
      //通过ref获取el-table的表头数据
      // console.log('高级查询————————————tableRef', tableRef.$children)
      let columnTitle = tableRef.$children
        .filter(x => {
          return x._props.prop !== undefined
        })
        .map(x => {
          return new Object({
            key: x.$vnode.key,
            value: x._props.prop,
            label: x._props.label
          })
        })
      this.headeroptions = []
      this.headerCondition = []
      this.headeroptions = columnTitle
      console.log('columnTitle', columnTitle)
      this.headerCondition.push(columnTitle)
      console.log('headerCondition', this.headerCondition)
      columnTitle.sort((a, b) => {
        return a.key - b.key
      })
      console.log('columnTitle', columnTitle)
      console.log('headerCondition', this.headerCondition)
    },
    //高级查询重置
    resetQuery() {
      this.superQueryFlag = false
      this.QueryLength = ['']
      this.relation = ''
      this.selectedHeader = ['']
      this.compara = ['']
      this.fieldValue = ['']
      this.$emit('resetQuery')
    },
    //点击查询
    QueryForData() {
      var queryArray = []
      for (var i = 0; i < this.QueryLength.length; i++) {
        if (this.selectedHeader[i] !== '' && this.compara[i] !== '' && this.fieldValue[i] !== '') {
          queryArray.push(
            new Object({
              name: this.selectedHeader[i],
              condition: this.compara[i],
              content: this.fieldValue[i]
            })
          )
        } else {
          this.superQueryFlag = false
          this.$emit('resetQuery')
        }
      }
      if (queryArray.length == 0) {
        return false
      }
      this.superQueryFlag = true
      this.$emit('getInquiryResult', queryArray, this.relation)
    },

    comparaChanged(index) {
      if (this.compara[index - 1] == '') {
        this.fieldValue[index - 1] = ''
      }
    },
    headerChanged(index) {
      if (this.selectedHeader[index - 1] == '') {
        this.compara[index - 1] = ''
        this.fieldValue[index - 1] = ''
      }
    },
    delCondition(index) {
      if (this.QueryLength.length === 1) return
      this.QueryLength.splice(index - 1, 1)
      this.comparaCondition.splice(index - 1, 1)
      this.headerCondition.splice(index - 1, 1)
      this.compara.splice(index - 1, 1)
      this.fieldValue.splice(index - 1, 1)
      this.selectedHeader.splice(index - 1, 1)
    },
    addCondition() {
      this.QueryLength.push('')
      this.comparaCondition.push(this.comparaOptions)
      this.headerCondition.push(this.headeroptions)
      this.compara.push('')
      this.fieldValue.push('')
      this.selectedHeader.push('')
    }
  }
}
</script>

<style>
.mzindex {
  z-index: 3000 !important;
}
</style>
